package independence.events
{
	import independence.core.IndyObject;
	
	/**
	 * The <code>EventListener</code> class defines a data object for keeping track of an
	 * event listener registered on an object. The <code>EventListener</code> class is used
	 * in conjunction with the <code>EventManager</code> class to maintain a record of all
	 * event listeners registered on an object for garbage-collection optimization.
	 * 
	 * @author	TK, TK Assembled for Independence Glory.
	 */
	public final class EventListener extends IndyObject
	{
		/**
		 * Holds the listener function for this <code>EventListener</code> instance.
		 */
		private var _listener:Function;
		/**
		 * The type of <code>Event</code> this <code>EventListener</code> is registered to listen for.
		 */
		private var _type:String;
		/**
		 * Whether to listen for the event in the capture phase or not.
		 */
		private var _useCapture:Boolean = false;
		/**
		 * The priority of the event listener.
		 */
		private var _priority:int = 0;
		/**
		 * Whether to use a weak reference in registering the event listener.
		 */
		private var _useWeakReference:Boolean = true;
		/**
		 * Constructs a new <code>EventListener</code> data object. 
		 */
		public function EventListener()
		{
			super();
		}
		/**
		 * Quickly sets all of the properties of this <code>EventListener</code> instance.
		 * @param	type		The type of <code>Event</code> associated with this event listener.
		 * @param	listener	The listener function for the <code>Event</code>.
		 * @param	useCapture	Whether or not to listen in the capture phase.
		 * @param	priority	The priority level of the event listener.
		 * @param	useWeakReference	Whether to use a weak reference in regard to the event listener, allowing the target object to be garbage collected.
		 */
		public function setup(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = true):void
		{
			this._type = type;
			this._listener = listener;
			this._useCapture = useCapture;
			this._priority = priority;
			this._useWeakReference = useWeakReference;
		}
		
		public function toString():String {
			return "[EventListener type=" + this.type + "]";
		}
		/**
		 * The listener function which is invoked when an <code>Event</code> is fired. 
		 */
		public function get listener():Function { return this._listener; }
		public function set listener(value:Function):void { this._listener = value; }
		
		public function get priority():int { return this._priority }
		public function set priority(value:int):void { this._priority = value; }
		/**
		 * The type of <code>Event</code> which this <code>EventListener</code> is registered for.
		 */
		public function get type():String { return this._type; }
		public function set type(value:String):void { this._type = value; }
		/**
		 * Whether to listen for the <code>Event</code> in the <code>capture</code> phase or not.
		 */
		public function get useCapture():Boolean { return this._useCapture; }
		public function set useCapture(value:Boolean):void { this._useCapture = value; }
		/**
		 * Whether to use a weak reference to the listener, allowing the object to be garbage collected even if this event listener is still
		 * registered to it.
		 */
		public function get useWeakReference():Boolean { return this._useWeakReference; }
		public function set useWeakReference(value:Boolean):void { this._useWeakReference = value; }
	}
}